﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

namespace Cinamon {
  public class Movie {

    String movieId;
    String movieName;
    String moviePicture;
    String movieLink;
    String movieBuyLink;
    String movieSoonest;
    DateTime movieDateTime;
    String movieRating;
    String movieDescription;
    String movieLength;
    String movieDateString;


    public String MovieDateString {
      get {
        return movieDateString;
      }
      set {
        DateTime date = DateTime.Parse(value);
        if (date.Date.Equals(DateTime.Now.Date)) {
          movieDateString = "TÄNA";
        } else if (date.Date.Equals(DateTime.Now.Date.AddDays(1))) {
          movieDateString = "Homme";
        } else {
          movieDateString = date.Day.ToString() + ". " + date.ToString("MMM");
        }
      }
    }


     public String MovieId {get; set;}

    public String MovieLength {get; set;}

    public String MovieDescription {get; set;}

    public String MovieRating {get; set;}

    public DateTime MovieDateTime {get; set;}
    
    public String MoviePicture {get; set;}

    public String MovieLink {get; set;}

    public String MovieBuyLink {get; set;}

    public String MovieSoonest {get; set;}

    // XML´is on täpitähtede asemel numbrid, see meetod:
    // Muudab numbrid täpitähtedes, kui need on sõna sees. 
    // Muudab esimese tähe suureks
    // Kui filmi nimes on kaks numbrit järjest, millest üks on "2", siis neid ei muudeta (21 jump street).
    // Kui filmi lõpus on tühik ja siis number, siis seda ei muudeta (järjefilmide puhul, nt. Piraadid 2).
    // TODO: Mingi parem moodus korrektselt filminime saamiseks.
    public String MovieName {
      get {
        return movieName;
      }
      set {
        Char[] chars = new Char[] { '2', 'y', '6', '8' };
        Char[] accented = new Char[] { 'ä', 'ü', 'õ', 'ö' };
        Char[] temp = value.ToCharArray();
        int tempLength = temp.Length;
        if (temp[temp.Length - 1] == '2' && temp[temp.Length - 2] == ' ') {
          tempLength -= 1;
        }
        int i = 0;
        if (char.IsNumber(temp[0]) && char.IsNumber(temp[1]) && temp[2] == ' ') {
          i += 2;
        }
        for (int m = i; i < tempLength; i++) {
          for (int j = 0; j < chars.Length; j++) {
            if (temp[i] == chars[j]) {
              temp[i] = accented[j];
            }
          }
        }
        temp[0] = char.ToUpper(temp[0]);
        string replacedValue = new String(temp);
        movieName = replacedValue;
      }
    }

    

  }


  // Kasutatakse pealehel filmide ühekordseks kuvamiseks
  class MovieComparer : IEqualityComparer<Movie> {

    public bool Equals(Movie x, Movie y) {
      if (Object.ReferenceEquals(x, y))
        return true;
      if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
        return false;
      return x.MovieName == y.MovieName && x.MovieLink == y.MovieLink;
    }

    public int GetHashCode(Movie movie) {
      if (Object.ReferenceEquals(movie, null))
        return 0;
      int hashMovieName = movie.MovieName == null ? 0 : movie.MovieName.GetHashCode();
      return hashMovieName;
    }

  }
}
